package com.gcsoftware.web.controller.admin;

import com.gcsoftware.web.service.T_GW_COMPANY_SERVICE;
import com.gcsoftware.web.beans.T_GW_COMPANY;
import com.gcsoftware.utils.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by qs on 2018/3/19.
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private T_GW_COMPANY_SERVICE service;

    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    public @ResponseBody List<T_GW_COMPANY> queryForList(@RequestParam(value = "c_COMPANY_NAME" , required = false) String c_COMPANY_NAME) {
        return service.queryForList(c_COMPANY_NAME);
    }

    @RequestMapping(value = "/{c_COMPANY_ID}/delete" , method = RequestMethod.POST)
    @ResponseBody
    public MapUtils remove(@PathVariable("c_COMPANY_ID") String c_COMPANY_ID) {
        return  1 == service.delete(c_COMPANY_ID) ? MapUtils.ok() : MapUtils.error();
    }

    @RequestMapping(value = "/save" , method =  RequestMethod.POST)
    @ResponseBody
    public MapUtils save(T_GW_COMPANY company) {
        if (StringUtils.isBlank(company.getC_COMPANY_ID())) {
            return MapUtils.error("未能获取厂家信息，请刷新页面后重试！");
        }
        if (StringUtils.isBlank(company.getC_COMPANY_NAME())) {
            return MapUtils.error("厂家名称不能为空！");
        }
        return 1 == service.update(company) ? MapUtils.ok() : MapUtils.error("修改厂家失败！");
    }

}
